<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/9/18
 * Time: 11:52
 */

namespace app\manage\controller;
class HouseType extends Base
{
    private $house_id;
    public function _initialize(){
        $this->house_id = input('param.house_id/d',0);
        $this->param_extra = ['house_id'=>$this->house_id];
        parent::_initialize();
        $this->mod = model('house_type');
        $this->sort = 'ordid';
        $this->order = 'asc,id desc';
        $this->getHouseName();
        $this->assign('house_id',$this->house_id);
    }
//搜索
    public function _search(){
        $map['house_id'] = $this->house_id;
        return $map;
    }
    //添加户型
    public function addDo(){
       \app\common\model\HouseType::event('before_insert',function($obj){
           $images = $this->getPic();
           if($images){
               $obj->img = $images[0]['url'];
               $obj->file = $images;
           }
           return true;
       });
        parent::addDo();
    }

    /**
     * 编辑户型
     */
    public function editDo(){
        \app\common\model\HouseType::event('before_update',function($obj){
            $images = $this->getPic();
            if($images){
                $obj->img = $images[0]['url'];
                $obj->file = $images;
            }
            return true;
        });
        parent::editDo();
    }
    /**
     * @param $obj
     * 添加图片
     */
    private function getPic(){
        $insert = [];
        if(isset($_POST['pic']) && !empty($_POST['pic'])) {
            $images = $_POST['pic'];
            foreach ($images['pic'] as $key => $v) {
                $insert[] = [
                    'url' => $v,
                    'title' => $images['alt'][$key],
                ];
            }
        }
        return $insert;
    }
    //获取楼盘名称
    private function getHouseName(){
        $title = model('house')->where(['id'=>$this->house_id])->value('title');
        $this->assign('house_title',$title);
    }
    public function ajaxCheckTitle(){
        $title = input('get.J_title');
        $id    = input('param.id/d',0);
        $return['status'] = 1;
        $where['title'] = $title;
        $id && $where['id'] = ['neq',$id];
        $count = $this->mod->where($where)->count();
        if($count > 0){
            $return['status'] = 0;
        }
        return json($return);
    }
}